<template>
    <div class="login">
      <div class="form">
        <div class="title">
          智慧点餐平台
        </div>
        <el-form :model="formData" :rules="rules" ref="formRef">
          <el-form-item prop="account">
            <el-input v-model="formData.account" placeholder="请输入账号">
              <template #prefix>
                <el-icon class="el-input__icon">
                  <UserFilled color="#fff" />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="formData.password" type="password" show-password placeholder="请输入密码">
              <template #prefix>
                <el-icon class="el-input__icon">
                  <Lock color="#fff" />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">立即登录</el-button>
          </el-form-item>
  
        </el-form>
      </div>
    </div>
  </template>
  
  <script setup>
  import { reactive, onMounted, ref } from 'vue'
  import { useRouter } from 'vue-router'
  
  import { UserFilled, Lock } from '@element-plus/icons-vue'
  import { checkLogin } from '@/api/account'
  import { ElMessage } from 'element-plus'
  const router = useRouter()
  //登录表单数据
  const formData = reactive({
    account: '',
    password: ''
  })
  
  const formRef = ref(null);
  
  //触发的校验事件函数
  const validateAccount = (rule, value, callback) => {
    //rule :整个input的所有的规则  
    //value:当前input的值
    //callback:回调函数，如果校验通过，callback() ，  必须只要写一次验证通过
    //校验不通过，callback(new Error('错误信息'))
    let reg = /^([a-z]|[0-9]){4,11}$/
    if (reg.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的账号'))
    }
  
  }
  const validatePassword = (rule, value, callback) => {
    let reg = /^([a-z]|[0-9]){4,11}$/
    if (reg.test(value)) {
      callback()
    } else {
      callback(new Error('请输入正确的密码'))
    }
  }
  
  //校验规则
  const rules = reactive({
    account: [
      { validator: validateAccount, trigger: 'blur' }
    ],
    password:[
      {validator: validatePassword, trigger: 'blur' }
    ]
  })
  
  
  // 调用层
  const login = () => {
  // 触发表单的自定义验证 通过后才可调用接口
  formRef.value.validate(async (res) => {
    if (res) {
      let { code, msg, id, role, token } = await checkLogin(formData);
      if (code == 0) {
        //存用户登录基本数据到pinia 
        // store.changeUserInfo({id,role,token})
        sessionStorage.setItem('user', JSON.stringify({ id, role, token }))
        ElMessage.success(msg)
        router.push('/home')
      } else {
        ElMessage.error(msg)
      }
    }
  })
}
  </script>
  
  <style scoped lang="scss">
  .login {
    width: 100%;
    height: 100%;
    background-color: #2d3a4b;
  }
  
  .form {
    width: 400px;
    height: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  
  .title {
    width: 100%;
    height: 50px;
    color: #fff;
    font-size: 20px;
    text-align: center;
    line-height: 50px
  }
  
  :deep(.el-input__wrapper) {
    background-color: transparent;
  }
  
  :deep(.el-input__inner){
  color: #fff;
}
  </style>